package com.zdd.ehe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdd.ehe.entity.DiseaseData;
import com.zdd.ehe.entity.Role;
import com.zdd.ehe.entity.User;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.IDiseaseDataService;
import com.zdd.ehe.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhuiZhu
 * @since 2023-03-24
 */
@Api(tags="病虫害信息管理模块")
@RestController
@RequestMapping("/diseaseData")
@Slf4j
public class DiseaseDataController {

    @Resource
    private IDiseaseDataService diseaseDataService;

    @ApiOperation("分页获取病虫害信息接口")
    @PostMapping("/getalldiseasebypage")
    public Result getAllDiseaseByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        Page<DiseaseData> pagex = new Page<>(page,pageSize);
        Page<DiseaseData> pageResult = diseaseDataService.getAllDiseaseByPage(pagex);
        if(pageResult.getSize() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pageResult);
    }

    @ApiOperation("通过diseasename获取病虫害列表接口")
    @GetMapping("/getDiseaseData")
    public Result getDiseaseData(@RequestParam("name") String name){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("name", name);
        List<DiseaseData> diseaseDataList = diseaseDataService.list(wrapper);
        //log.info("diseaseList" + diseaseDataList);
       /* if(diseaseDataList.size() != 0){
            Map<String,Object> data = new HashMap<>();
            data.put("diseaseDataList",diseaseDataList);
            data.put("total",data.size());
            return Result.success("查询成功!",data);
        }else{
            return Result.error(4001,"未查询到该病虫害信息");
        }*/
        return Result.success("查询成功",diseaseDataList);
    }

    @ApiOperation("删除病虫害信息接口")
    @PostMapping("/deletediseasedatabyid")
    public Result deleteUserById(@RequestParam("diseaseId") Integer diseaseId){
        DiseaseData diseaseData = diseaseDataService.getById(diseaseId);
        if(diseaseData == null){
            return Result.error(4001,"编号错误");
        }
        diseaseData.setStatus("0");
        int result = diseaseDataService.update(diseaseData);
        if(result == -1){
            return Result.error(4002,"删除失败");
        }
        return Result.success("删除成功");
    }

    @ApiOperation("重新启用病虫害信息状态接口")
    @PostMapping("/enablediseasedatabyid")
    public Result enableDiseaseDataById(@RequestParam("diseaseId") Integer diseaseId){
        DiseaseData diseaseData = diseaseDataService.getById(diseaseId);
        if(diseaseData == null){
            return Result.error(4001,"编号错误");
        }
        diseaseData.setStatus("1");
        int result = diseaseDataService.update(diseaseData);
        if(result == -1){
            return Result.error(4002,"重新启用失败");
        }
        return Result.success("重新启用成功");
    }

    @ApiOperation("添加病虫害信息")
    @PostMapping("/addDiseaseData")
    public Result addDiseaseData(@RequestBody DiseaseData diseaseData){
        log.info("diseasedata: " + diseaseData);
        int d =diseaseDataService.addDiseaseData(diseaseData);
        if(d == 1){
            return Result.success("添加成功");
        }else {
            return Result.error(4002,"添加失败");
        }
    }

    @ApiOperation("修改病虫害信息")
    @PostMapping("/updatediseadata")
    public Result updateDiseaseData(@RequestBody DiseaseData diseaseData){
        int updatediseasedata = diseaseDataService.update(diseaseData);
        if(updatediseasedata == -1){
            return Result.error("修改错误");
        }
        return Result.success("修改成功");
    }
}
